<template>
  <a-modal
      title="添加部门"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      okText="保存"
      cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="dataForm" :model="dataForm" :rules="validatorRules">
        <a-form-model-item label="部门名称" prop="name" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-model="dataForm.name" />
        </a-form-model-item>
        <a-form-model-item label="上级部门" prop="pid" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <select-tree :options="departTree" v-model="dataForm.pid"></select-tree>
        </a-form-model-item>
        <a-form-model-item label="排序" prop="sort" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-model="dataForm.sort" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { addDepart, duplicateCheck } from '@/api/api'

export default {
  name: "dept-form",
  data () {
    return {
      departTree: [],
      dataForm: {
        pid: '0',
        name: '',
        sort: 0,
      },
      visible: false,
      confirmLoading: false,
      replaceFields: {
        children: 'children',
        title: 'name',
        key: 'id',
        value: 'id'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
    }
  },
  computed: {
    validatorRules () {
      var validateName = (rule, value, callback) => {
        var params = {
          tableName: 'sys_dept',
          fieldName: 'name',
          fieldVal: value,
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback("部门名称已存在!")
          }
        })
      }
      return {
        name: [
          { required: true, message: '请输入部门名称!' },
          { validator: validateName, trigger: 'blur' }
        ],
        pid: [{ required: true, message: '请选择上级部门!', trigger: 'change' }],
        sort: [{ required: true, message: '请选择排序序号!' }],
      }
    },
  },
  methods: {
    init () {
      this.visible = true
      const dataForm = Object.assign({}, this.dataForm)
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm = dataForm
      })
    },
    handleOk () {
      const that = this;
      // 触发表单验证
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          addDepart(that.dataForm).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok');
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.handleCancel();
          });
        }
      })
    },
    handleCancel () {
      this.visible = false;
    }
  }
}
</script>

<style scoped>

</style>
